package com.common.link.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.common.link.cache.AtlasCache;
import com.common.link.cache.AtlasCacheService;
import com.common.link.config.LinkConfig;
import com.common.link.model.EntityVO;
import com.common.link.model.RelationVO;
import com.common.link.model.RemoteResult;
import com.service.http.service.HttpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 *
 * @author xianggj
 * @Date 2022/2/14 17:20
 **/
public class RemoteAtlasService {

    @Autowired
    private HttpService httpService;

    @Autowired
    private LinkConfig linkConfig;

    @Autowired
    private AtlasCacheService atlasCacheService;

    /**
     * 获取所有实体类型
     * @param token
     * @return
     */
    public List<EntityVO> getEntityList(String token){

        String entityListUrl = linkConfig.getEntityList();
        HttpHeaders headers = new HttpHeaders();
        headers.add(LinkConfig.HEADER, token);
        String s = httpService.get(entityListUrl, headers);
        RemoteResult remoteResult = JSON.parseObject(s, RemoteResult.class);
        List<EntityVO> resList = new ArrayList<>();
        if (remoteResult == null){
            return resList;
        }
        String data = remoteResult.getData();
        if (data == null ){
            return resList;
        }
        JSONArray jsonArray = JSON.parseArray(data);
        for (Object o : jsonArray){
            JSONObject jo = (JSONObject) o;
            String id = jo.getString("id");
            EntityVO entityVO = new EntityVO(id,
                    jo.getString("entityName"), jo.getString("entityCode"));
            JSONObject style = jo.getJSONObject("style");
            if (style != null ){
                String color = style.getString("color");
                entityVO.setColor(color);
            }
            resList.add(entityVO);
            atlasCacheService.updateEntityById(id, entityVO);
        }
        return resList;
    }

    /**
     * 获取所有关系类型
     * @param token
     * @return
     */
    public List<RelationVO> getRelationList(String token){
        String relationListUrl = linkConfig.getRelationList();
        HttpHeaders headers = new HttpHeaders();
        headers.add(LinkConfig.HEADER, token);
        String s = httpService.get(relationListUrl, headers);
        RemoteResult remoteResult = JSON.parseObject(s, RemoteResult.class);
        List<RelationVO> resList = new ArrayList<>();
        if (remoteResult == null){
            return resList;
        }
        String data = remoteResult.getData();
        if (data == null ){
            return resList;
        }
        JSONArray jsonArray = JSON.parseArray(data);
        for (Object o : jsonArray){
            JSONObject jo = (JSONObject) o;
            resList.add(new RelationVO(jo.getString("id"),
                    jo.getString("relationName"), jo.getString("relationCode")));
            AtlasCache.set(jo.getString("id"), jo.getString("relationName"));
        }
        return resList;
    }

}
